package com.mililoan.common.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;
import org.springframework.jdbc.core.RowCallbackHandler;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;

import com.mililoan.common.exception.PersistenetException;

public class BaseDao extends JdbcDaoSupport{

	private DataSource dataSource;
	//事务管理器
	private PlatformTransactionManager transactionManager;
	/**
	 * 得到序列号的下一个值
	 * */
	public int getSeqNextVal(String p_seq) throws DataAccessException
	{
		String sql="select nextval('"+p_seq+"') val from dual";

		JdbcTemplate template = this.getJdbcTemplate();

		return template.queryForInt(sql);
		
	}
	public Connection getNotAutoCommitConnection() throws PersistenetException
	{
		try {
			
			Connection result=this.getConnection();
			result.setAutoCommit(false);
			return result;
		} catch (SQLException e) {
			e.printStackTrace();
			throw new PersistenetException(
					"CommonDao 创建连接时异常：" + e.getMessage());
		}
	}

	public int update(Connection con,String sql, PreparedStatementSetter pss) throws PersistenetException
	{
		PreparedStatement ps=null;
		try {
			 ps=con.prepareStatement(sql);
			 pss.setValues(ps);
			 
			 return ps.executeUpdate();
			
			
		} catch (SQLException e) {
			
			e.printStackTrace();
			throw new PersistenetException(
					"CommonDao 执行update时异常：" + e.getMessage());
		}
		finally
		{
			if(ps!=null)
			{
				try {
					ps.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		
	}
	public void query(Connection con,String sql,Object[] arg, RowCallbackHandler rch) throws PersistenetException
	{
		PreparedStatement pstmt=null;
		ResultSet rs=null;
		
		
		try {
			pstmt=con.prepareStatement(sql);
			
			if(arg!=null&&arg.length>0)
			{
				for(int i=0;i<arg.length;i++)
				{
					Object o=arg[i];
					if(String.class==o.getClass())
					{
						pstmt.setString(i+1,(String)o);
					}
					else if(Integer.class==o.getClass())
					{
						pstmt.setInt(i+1,((Integer)o).intValue());
					}
					else if(Long.class==o.getClass())
					{
						pstmt.setInt(i+1,((Long)o).intValue());
					}
					else 
					{
						throw new PersistenetException("query时,参数类型不对异常：");
					}
				}
			}
			
			
			rs=pstmt.executeQuery();
			
			while(rs.next())
			{
				rch.processRow(rs);
			}
			
			
			
		} catch (SQLException e) {
		
			e.printStackTrace();
			throw new PersistenetException(
					"CommonDao 执行query时异常：" + e.getMessage());
		}
		finally
		{
			try {
				if(pstmt!=null)
					pstmt.close();
				if(rs!=null)
					 rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
	public void closeConnection(Connection con) throws PersistenetException
	{
		try {
			con.close();
		} catch (SQLException e) {
			
			e.printStackTrace();
			throw new PersistenetException(
					"CommonDao 关闭连接时异常：" + e.getMessage());
		}
	}
	public void commitConnection(Connection con) throws PersistenetException
	{
		try {
			con.commit();
			con.setAutoCommit(true);
		} catch (SQLException e) {
			
			e.printStackTrace();
			throw new PersistenetException(
					"CommonDao 关闭连接时异常：" + e.getMessage());
		}
	}
}

